package com.ruoyi.project.sell.order.service;

import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.security.ShiroUtils;
import com.ruoyi.common.utils.text.Convert;
import com.ruoyi.project.sell.order.domain.SellOrder;
import com.ruoyi.project.sell.order.mapper.OrderMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class OrderServiceImpl implements OrderService {
    @Autowired
    private OrderMapper orderMapper;
    @Override
    public List<SellOrder> selectSellOrderService(SellOrder sellOrder) {
        return orderMapper.selectSellOrderMapper(sellOrder);
    }

    @Override
    public String checkOrderNameUnique(SellOrder sellOrder) {
        Long orderId = StringUtils.isNull(sellOrder.getOrderId()) ? -1L : sellOrder.getOrderId();
        SellOrder info = orderMapper.checkOrderNameUnique(sellOrder.getItemName());
        if (StringUtils.isNotNull(info) && info.getOrderId().longValue() != orderId.longValue())
        {
            return UserConstants.Order_NAME_NOT_UNIQUE;
        }
        return UserConstants.Order_NAME_UNIQUE;
    }

    @Override
    public int insertSellOrder(SellOrder sellOrder) {
        sellOrder.setUserName(ShiroUtils.getLoginName());
        return orderMapper.insertSellOrderService(sellOrder);
    }

    @Override
    public int deleteSellOrderByIds(String ids) {
        Long[] orderIds = Convert.toLongArray(ids);
        return orderMapper.deleteSellOrderByIds(orderIds);
    }

    @Override
    public SellOrder selectOrderById(Long orderId) {
        return orderMapper.selectOrderByIdMapper(orderId);
    }

    @Override
    public int updateSellOrderService(SellOrder sellOrder) {
        return orderMapper.updateSellOrderById(sellOrder);
    }
}
